package com.example.myapp;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.myapp.adapter.MainVpAdapter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class FragmentActivity extends AppCompatActivity {

    private ViewPager mVp;
    private Button homeview;
    private Button centerview;
    private Button leftview;
    private  DemoFragment topFragment;
    private  DemoFragment bottomFragment;
    private MainVpAdapter mainVpAdapter;

    private static final String TAG = "MainActivity";
    ViewPager viewPager;
    BottomNavigationView navigation;
    List<Fragment> listFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity);
        initView();
    }


//        mVp=findViewById(R.id.m_content);
//        homeview=findViewById(R.id.btn_4);
//        centerview=findViewById(R.id.btn_5);
//        leftview=findViewById(R.id.btn_6);
//
//        mainVpAdapter = new MainVpAdapter(getSupportFragmentManager());
//        mVp.setAdapter(mainVpAdapter);
//
//        homeview.setOnClickListener(view->mVp.setCurrentItem(0));
//
//        centerview.setOnClickListener(view->mVp.setCurrentItem(1));
//
//        leftview.setOnClickListener(view->mVp.setCurrentItem(2));

        private void initView(){
            viewPager = (ViewPager) findViewById(R.id.view_content);
            navigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);

            listFragment = new ArrayList<>();
            listFragment.add(new DemoFragment1());
            listFragment.add(new DemoFragment2());
            listFragment.add(new DemoFragment3());
            MainVpAdapter myAdapter = new MainVpAdapter(getSupportFragmentManager(),this,listFragment);
            viewPager.setAdapter(myAdapter);

            navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.navigation_home:
                            viewPager.setCurrentItem(0);
                            return true;
                        case R.id.navigation_dashboard:
                            viewPager.setCurrentItem(1);
                            return true;
                        case R.id.navigation_notifications:
                            viewPager.setCurrentItem(2);
                            return true;
                        default:
                            break;
                    }
                    return false;
                }
            });

            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    navigation.getMenu().getItem(position).setChecked(true);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
        }


//        Log.e("tag","onCreate");
//        topFragment =new DemoFragment();
//        bottomFragment =new DemoFragment();
//        getFragmentManager().beginTransaction().replace(R.id.b_1,topFragment).commit();
//        getFragmentManager().beginTransaction().replace(R.id.b_2,bottomFragment).commit();
////        getSupportFragmentManager();

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("tag","onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("tag","onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("tag","onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("tag","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("tag","onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("tag","onDestroy");
    }
}
